package xy.come.juhenews.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import xy.come.juhenews.Adapter.GirlListAdapter;
import xy.come.juhenews.Base.BaseActivity;
import xy.come.juhenews.Bean.GirlListBean;
import xy.come.juhenews.Http.Constant;
import xy.come.juhenews.R;
import xy.come.juhenews.Uitils.LoadingDialog;
import xy.come.juhenews.Uitils.LogUtils;

/**
 * Created by Xia_焱 on 2018/7/31.
 * e-mail：XiahaotianV@163.com
 * 美女列表
 */

public class GirlPhotoActivity extends BaseActivity {
    @BindView(R.id.ll_head_back)
    LinearLayout llHeadBack;
    @BindView(R.id.rc_girl_list)
    RecyclerView rcGirlList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private GirlListAdapter mAdapter;
    private LoadingDialog loadingDialog;
    private ArrayList<GirlListBean.DataBean> girlDataList;
    private static final int PAGE_SIZE = 8;
    private int mStartPage = 1;
    private View errorView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_girl_photo;
    }

    @Override
    protected void initView() {

        StaggeredGridLayoutManager recyclerViewLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rcGirlList.setLayoutManager(recyclerViewLayoutManager);
        loadingDialog = new LoadingDialog(GirlPhotoActivity.this);
        errorView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) rcGirlList.getParent(), false);
        initAdapter();
        refresh();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(GirlPhotoActivity.this, PhotosDetailActivity.class);
                intent.putExtra("girl", girlDataList.get(position).getUrl());
                startActivity(intent);


            }
        });

        llHeadBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void initAdapter() {
        mAdapter = new GirlListAdapter(R.layout.item_girl_list, girlDataList);
        mAdapter.openLoadAnimation();
        rcGirlList.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        });
    }


    private void refresh() {
        mStartPage = 1;
        loadingDialog.LoadingShow();
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        OkHttpUtils.get()
                .url(Constant.GIRL_LIST + mStartPage + "/count/12").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mAdapter.loadMoreFail();
                loadingDialog.LoadingDismiss();
            }

            @Override
            public void onResponse(String response, int id) {
                LogUtils.e(response);
                GirlListBean girlListBean = JSON.parseObject(response, GirlListBean.class);
                List<GirlListBean.DataBean> data = girlListBean.getData();

                if (data != null) {
                    girlDataList.clear();
                    girlDataList.addAll(data);
                    setData(true, girlDataList);
                    mAdapter.setEnableLoadMore(true);
                    swipeLayout.setRefreshing(false);
                    loadingDialog.LoadingDismiss();
                }
                loadingDialog.LoadingDismiss();

            }
        });
    }


    private void loadMore() {
        OkHttpUtils.get()
                .url(Constant.GIRL_LIST + mStartPage + "/count/12").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mAdapter.loadMoreFail();

            }

            @Override
            public void onResponse(String response, int id) {
                LogUtils.e(response);
                GirlListBean girlListBean = JSON.parseObject(response, GirlListBean.class);
                List<GirlListBean.DataBean> data = girlListBean.getData();

                if (data != null) {
                    girlDataList.addAll(data);
                    setData(false, girlDataList);
                }

            }
        });
    }


    @Override
    protected void initData() {
        girlDataList = new ArrayList<>();
        initRefreshLayout();
    }

    private void setData(boolean isRefresh, ArrayList<GirlListBean.DataBean> list) {
        mStartPage++;
        final int size = list == null ? 0 : list.size();
        if (isRefresh) {
            mAdapter.setNewData(list);

        } else {
            if (size > 0) {
                mAdapter.addData(list);
            } else {
                mAdapter.setEmptyView(errorView);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);

        } else {
            mAdapter.loadMoreComplete();
        }
    }

    private void initRefreshLayout() {
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

}
